define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/teacherevaluation/index',
                    add_url: 'user/teacherevaluation/add',
                    edit_url: 'user/teacherevaluation/edit',
                    del_url: 'user/teacherevaluation/del',
                    multi_url: 'user/teacherevaluation/multi',
                    table: 'teacher_evaluation',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: 'ID', operate: false},
                        {field: "city_id", title: "城市" },
                        {field: 'school_id', title: '学校'},
                        {field: 'student.nickname', title: '学生', operate: 'LIKE'},
                        {field: 'teacher.nickname', title: '老师', operate: 'LIKE'},
                        {field: 'class.name', title: '班级', operate: 'LIKE'},
                        {field: 'rating_text', title: '评分', operate: false},
                        {field: 'content', title: '评价内容', operate: 'LIKE', formatter: function(value, row, index) {
                            if (value && value.length > 50) {
                                return value.substr(0, 50) + '...';
                            }
                            return value || '';
                        }},
                        {field: 'is_anonymous_text', title: '是否匿名', operate: false},
                        {field: 'create_time', title: '创建时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    // 获取URL中的teacher_id参数
                    var urlParams = new URLSearchParams(window.location.search);
                    var teacherId = urlParams.get('teacher_id');
                    if (teacherId) {
                        params.teacher_id = teacherId;
                    }
                    return params;
                }
            });
            
            // 引入城市学校过滤功能
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js?';
            document.head.appendChild(script);
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        edit: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});